package com.rongxun.hbwallet.ui.activities;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongxun.base.MvpActivity;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.constants.IntentKey;
import com.rongxun.hbwallet.mvp.contract.IWalletDetail;
import com.rongxun.hbwallet.mvp.presenter.WalletDetailPresenter;
import com.rongxun.utils.EventConf;
import com.rongxun.utils.EventUtil;
import com.rongxun.utils.FlyLog;
import com.rongxun.utils.Md5Utils;
import com.rongxun.utils.StatusBarUtil;
import com.rongxun.utils.ToastUtils;
import com.rongxun.utils.wallet.ETHWallet;
import com.rongxun.view.InputPwdDialog;
import com.rongxun.widget.LoadingDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by dwq on 2018/3/27/027.
 * e-mail:lomapa@163.com
 */

public class ModifyPasswordActivity extends MvpActivity<IWalletDetail.IView, WalletDetailPresenter> implements IWalletDetail.IView, TextWatcher {
    @BindView(R.id.toolbar_title)
    TextView  mToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar   mToolbar;
    @BindView(R.id.et_old_pwd)
    EditText  etOldPwd;
    @BindView(R.id.et_new_pwd)
    EditText  etNewPwd;
    @BindView(R.id.et_new_pwd_again)
    EditText  etNewPwdAgain;
    @BindView(R.id.rl_btn)
    TextView  rlBtn;
    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;

    private long           walletId;
    private String         walletPwd;
    private String         walletAddress;
    private String         walletName;
    private boolean        walletIsBackup;
    private InputPwdDialog inputPwdDialog;
    private String         walletMnemonic;
    private Dialog         loadingDialog;
    private ETHWallet      mEthWallet;

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showLoading(boolean show) {
        if (show)
            loadingDialog.show();
        else
            loadingDialog.cancel();
    }

    @Override
    public void showError(String msg, String errorCode) {

    }

    @Override
    public void checkPermissionsSuccessful(String... permissions) {

    }

    @Override
    public void release() {
        if (loadingDialog != null) {
            loadingDialog.cancel();
        }
    }

    @NonNull
    @Override
    public WalletDetailPresenter createPresenter() {
        return new WalletDetailPresenter(this);
    }

    @Override
    protected void init() {
        StatusBarUtil.setToolBarPaddingTop(mToolbar, this);
        mToolbarTitle.setText(getString(R.string.modify_password_title));
        mToolbarBack.setVisibility(View.VISIBLE);
        loadingDialog = LoadingDialog.createLoadingDialog(this, getString(R.string.load), false);
        Intent intent = getIntent();
        mEthWallet = intent.getParcelableExtra(IntentKey.ETH_WALLET);

        walletId = mEthWallet.getId();
        walletPwd = mEthWallet.getPassword();
        walletAddress = mEthWallet.getAddress();
        walletName = mEthWallet.getName();
        walletIsBackup = mEthWallet.getIsBackup();
        walletMnemonic = mEthWallet.getMnemonic();

        FlyLog.i("walletPwd:" + walletPwd);

        etOldPwd.addTextChangedListener(this);
        etNewPwd.addTextChangedListener(this);
        etNewPwdAgain.addTextChangedListener(this);
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_modify_password;
    }

    @OnClick({R.id.toolbar_back, R.id.rl_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_back:
                onBackPressed();
                break;
            case R.id.rl_btn:
                String oldPwd = etOldPwd.getText().toString().trim();
                String newPwd = etNewPwd.getText().toString().trim();
                String newPwdAgain = etNewPwdAgain.getText().toString().trim();
                if (verifyPassword(oldPwd, newPwd, newPwdAgain)) {


                    getPresenter().modifyWalletPwd(walletId, walletName, oldPwd, newPwd);
                }
                break;
        }
    }

    private boolean verifyPassword(String oldPwd, String newPwd, String newPwdAgain) {
        FlyLog.i(oldPwd);
        FlyLog.i(Md5Utils.md5(oldPwd));

        if (!TextUtils.equals(Md5Utils.md5(oldPwd), walletPwd)) {
            ToastUtils.showToast(R.string.modify_password_alert4);
            return false;
        } else if (!TextUtils.equals(newPwd, newPwdAgain)) {
            ToastUtils.showToast(R.string.modify_password_alert5);
            return false;
        }
        return true;
    }

    @Override
    public void modifySuccess() {

    }

    @Override
    public void modifyPwdSuccess(ETHWallet ethWallet) {
        FlyLog.i("modifyPwd ethWallet:" + ethWallet);

        ToastUtils.showToast(R.string.modify_password_success);
        EventUtil.postInfoEvent(EventConf.MODIFY_WALLET_PASSWORD, ethWallet.getPassword());
        finish();
    }

    @Override
    public void showDerivePrivateKeyDialog(String privateKey) {

    }

    @Override
    public void showDeriveKeystore(String keystore) {

    }

    @Override
    public void deleteSuccess(boolean isDelete) {

    }

    @Override
    public void showQRCode(byte[] bytes) {

    }

    @Override
    public void showDeleteWalletResult() {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (etOldPwd.getText() != null
                && etNewPwd.getText() != null
                && etNewPwd.getText().toString().length() >= 8
                && etNewPwdAgain.getText() != null) {
            rlBtn.setEnabled(true);
            rlBtn.setTextColor(Color.parseColor("#D8B267"));
        } else {
            rlBtn.setEnabled(false);
            rlBtn.setTextColor(Color.WHITE);
        }


    }
}
